Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@thi.ng/errors
Advanced tools
@thi.ng/errors is a utility library for creating and handling custom error types in JavaScript. It provides a set of predefined error classes and functions to simplify error handling and improve code readability.
Custom Error Classes
This feature allows you to create and use custom error classes like IllegalArgumentError and IllegalStateError to handle specific error conditions in your code.
const { IllegalArgumentError, IllegalStateError } = require('@thi.ng/errors');
function checkAge(age) {
if (age < 0) {
throw new IllegalArgumentError('Age cannot be negative');
}
if (age > 120) {
throw new IllegalStateError('Age seems unrealistic');
}
return 'Age is valid';
}
try {
console.log(checkAge(-5));
} catch (e) {
console.error(e.message);
}
Predefined Error Types
The package provides predefined error types like UnsupportedOperationError to handle common error scenarios in a standardized way.
const { UnsupportedOperationError } = require('@thi.ng/errors');
function notImplemented() {
throw new UnsupportedOperationError('This operation is not supported');
}
try {
notImplemented();
} catch (e) {
console.error(e.message);
}
Error Wrapping
The wrapError function allows you to wrap existing errors with additional context, making it easier to debug and understand the root cause of errors.
const { wrapError } = require('@thi.ng/errors');
function riskyOperation() {
try {
throw new Error('Something went wrong');
} catch (e) {
throw wrapError(e, 'Risky operation failed');
}
}
try {
riskyOperation();
} catch (e) {
console.error(e.message);
}
create-error-class is a utility for creating custom error classes in JavaScript. It is similar to @thi.ng/errors in that it allows for the creation of custom error types, but it does not provide predefined error types or error wrapping functionality.
verror is a library for creating and manipulating chained errors. It provides more advanced error wrapping and chaining capabilities compared to @thi.ng/errors, making it suitable for complex error handling scenarios.
custom-error-generator is a simple utility for generating custom error classes. It is similar to @thi.ng/errors in terms of creating custom error types but lacks the predefined error types and error wrapping features.
[!NOTE] This is one of 199 standalone projects, maintained as part of the @thi.ng/umbrella monorepo and anti-framework.
🚀 Please help me to work full-time on these projects by sponsoring me on GitHub. Thank you! ❤️
Custom error types and error factory functions.
This package defines the following error types & helper functions to throw them:
AssertionError
IllegalArgumentError
IllegalArityError
IllegalStateError
OutOfBoundsError
UnsupportedOperationError
Custom error types can be easily defined using
defError()
.
STABLE - used in production
Search or submit any issues for this package
yarn add @thi.ng/errors
ESM import:
import * as err from "@thi.ng/errors";
Browser ESM import:
<script type="module" src="https://esm.run/@thi.ng/errors"></script>
For Node.js REPL:
const err = await import("@thi.ng/errors");
Package sizes (brotli'd, pre-treeshake): ESM: 817 bytes
None
One project in this repo's /examples directory is using this package:
Description | Live demo | Source |
---|---|---|
GPU-based data reduction using thi.ng/shader-ast & WebGL multi-pass pipeline | Demo | Source |
import * as err from "@thi.ng/errors";
err.illegalArity(3)
// Error: illegal arity: 3
err.illegalArgs("expected foo");
// Error: illegal argument(s): expected foo
err.illegalState("oops");
// Error: illegal state: oops
err.unsupported("TODO not yet implemented")
// Error: unsupported operation: TODO not yet implemented
// define custom error
const MyError = err.defError(
() => "Eeek... ",
(x) => x + " is not allowed!"
);
try {
throw new MyError(23);
} catch(e) {
console.warn(e.message);
console.log(e instanceof Error);
}
// Eeek... 23 is not allowed!
// true
The UMBRELLA_ASSERTS
or VITE_UMBRELLA_ASSERTS
env variables are
used to control the behavior of the assert()
function in production builds: If
either is set (to a non-empty string), the function will always be enabled.
Otherwise (by default), assert()
will be disabled for production builds,
i.e. if process.env.NODE_ENV === "production"
.
If this project contributes to an academic publication, please cite it as:
@misc{thing-errors,
title = "@thi.ng/errors",
author = "Karsten Schmidt",
note = "https://thi.ng/errors",
year = 2018
}
© 2018 - 2024 Karsten Schmidt // Apache License 2.0
FAQs
Custom error types and error factory functions
The npm package @thi.ng/errors receives a total of 318,568 weekly downloads. As such, @thi.ng/errors popularity was classified as popular.
We found that @thi.ng/errors demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.